<?php
class NewsAdminCatsAddForm extends Form
{
	function __construct()
	{				
		Form::Form('NewsAdminCatsAddForm');
	}
	
	function on_submit()
	{
		$name			=	Url::get('name');
		$public			=	Url::get('public',1);
		$position		=	Url::get('position','int',0);
		$keywords		=	Url::get('keywords');
		$description	=	Url::get('description');
		
		if($public != 1)
			$public = 0;
			
		$this->checkFormInput("Tên danh mục tin",'name',$name,'str',true,'',2,100);
		$this->checkFormInput("Vị trí",'position',$position,'int',true,'',1, DB::count("news_categories") + 1);
			
		if(!$this->errNum)
		{
			$id = DB::insert('news_categories', array(
														'name'			=>$name,
														'keywords'		=>$keywords,
														'description'	=>$description,
														'position'		=>$position,
														'public'		=>$public,
														'user_modify'	=>User::user_name(),
														'time_modify'	=>TIME_NOW,
														'user_create'	=>User::user_name(),
														'time_create'	=>TIME_NOW,
														));
			
			DB::query("UPDATE news_categories SET position=position+1 WHERE id!=".$id." AND position>=".$position);
				
			AZLib::getNewsCats(1);
			Url::redirect_current();
		}
	}
	
	function draw()
	{
		global $display;
		
		$this->beginForm();
		
		$display->add('name',Url::get('name',''));
		
		$max_pos = DB::count("news_categories") + 1;
		
		$display->add('position_option',AZLib::getOptionNum(1,$max_pos,Url::get('position',$max_pos)));
		
		$display->add('public', (int)Url::get('public',1));
		
		$display->add('keywords', Url::get('keywords',''));
		$display->add('description', Url::get('description',''));
		
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$display->output("NewsAdminCatsAdd");
		
		$this->endForm();
	}
}
?>